//
//  UITextField+ZB.m
//  PaoPao
//
//  Created by xzb on 2018/4/3.
//  Copyright © 2018年 paopao. All rights reserved.
//

#import "UITextField+ZB.h"
#import "UITextField+Blocks.h"
#import <objc/runtime.h>

static NSString *const kPasswordStr = @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890-_@!#;:'\",*.?+=&%$^~`[](){}|\\/";
static NSString *const kPhoneStr = @"1234567890";

static NSString *const kAccountStr = @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";

static const void *ZBTextFieldInputLimitMaxLength = &ZBTextFieldInputLimitMaxLength;

@implementation UITextField (ZB)

- (void)setupKeyboardType_Money
{
    self.keyboardType = UIKeyboardTypeDecimalPad;
}
- (void)setupKeyboardType_Phone
{
    self.keyboardType = UIKeyboardTypeNumberPad;
}
- (void)setupKeyboardType_Password
{
    self.keyboardType = UIKeyboardTypeASCIICapable;
}

- (void)setupStatus_bank
{
    self.keyboardType = UIKeyboardTypeNumberPad;
    [self setShouldChangeCharactersInRangeBlock:^BOOL(UITextField *textField, NSRange range, NSString *string) {
        NSString *text = [textField text];
        
        NSCharacterSet *characterSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789\b"];
        
        string = [string stringByReplacingOccurrencesOfString:@" " withString:@""];
        if ([string rangeOfCharacterFromSet:[characterSet invertedSet]].location != NSNotFound)
        {
            return NO;
        }
        
        text = [text stringByReplacingCharactersInRange:range withString:string];
        text = [text stringByReplacingOccurrencesOfString:@" " withString:@""];
        if ([text length] >= 21)
        {
            return NO;
        }
        
        NSString *newString = @"";
        while (text.length > 0)
        {
            NSString *subString = [text substringToIndex:MIN(text.length, 4)];
            newString = [newString stringByAppendingString:subString];
            if (subString.length == 4)
            {
                newString = [newString stringByAppendingString:@" "];
            }
            text = [text substringFromIndex:MIN(text.length, 4)];
        }
        
        newString = [newString stringByTrimmingCharactersInSet:[characterSet invertedSet]];
        
        [textField setText:newString];
        
        return NO;
    }];
}

- (void)setupStatus_CAPTCHA
{
    [self setupKeyboardType_Phone];
    [self setShouldChangeCharactersInRangeBlock:^BOOL(UITextField *textField, NSRange range, NSString *string) {
        if ([string isEqualToString:@""]) return YES;
        if (textField.text.length > (6 - 1)) return NO;
        if (![kPhoneStr containsString:string]) return NO;
        return YES;
    }];
}

- (void)setupStatus_Phone
{
    [self setupKeyboardType_Phone];
    [self setShouldChangeCharactersInRangeBlock:^BOOL(UITextField *textField, NSRange range, NSString *string) {
        if ([string isEqualToString:@""]) return YES;
        if (textField.text.length > (11 - 1)) return NO;
        if (![kPhoneStr containsString:string]) return NO;
        return YES;
    }];
}
- (void)setupStatus_Account
{
    [self setShouldChangeCharactersInRangeBlock:^BOOL(UITextField *textField, NSRange range, NSString *string) {
        if ([string isEqualToString:@""]) return YES;
        if (textField.text.length > (11 - 1)) return NO;
        if (![kAccountStr containsString:string]) return NO;
        return YES;
    }];
}
- (NSInteger)zb_maxLength {
    return [objc_getAssociatedObject(self, ZBTextFieldInputLimitMaxLength) integerValue];
}
- (void)setZb_maxLength:(NSInteger)maxLength {
    objc_setAssociatedObject(self, ZBTextFieldInputLimitMaxLength, @(maxLength), OBJC_ASSOCIATION_ASSIGN);
    [self addTarget:self action:@selector(zb_textFieldTextDidChange) forControlEvents:UIControlEventEditingChanged];
}
- (void)zb_textFieldTextDidChange {
    NSString *toBeString = self.text;
    //获取高亮部分
    UITextRange *selectedRange = [self markedTextRange];
    UITextPosition *position = [self positionFromPosition:selectedRange.start offset:0];
    
    //没有高亮选择的字，则对已输入的文字进行字数统计和限制
    //在iOS7下,position对象总是不为nil
    if ( (!position ||!selectedRange) && (self.zb_maxLength > 0 && toBeString.length > self.zb_maxLength))
    {
        NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:self.zb_maxLength];
        if (rangeIndex.length == 1)
        {
            self.text = [toBeString substringToIndex:self.zb_maxLength];
        }
        else
        {
            NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, self.zb_maxLength)];
            NSInteger tmpLength;
            if (rangeRange.length > self.zb_maxLength) {
                tmpLength = rangeRange.length - rangeIndex.length;
            }else{
                tmpLength = rangeRange.length;
            }
            self.text = [toBeString substringWithRange:NSMakeRange(0, tmpLength)];
        }
    }
}


@end
